package com.cssy.yd.logic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;



import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.logic.dao.LogicWhCheckDAO;
import com.cssy.yd.logic.dao.LogicWhCheckItemDAO;
import com.cssy.yd.logic.dao.LogicWhCheckMissionDAO;
import com.cssy.yd.logic.dao.LogicWhCheckMissionItemDAO;
import com.cssy.yd.logic.entity.LogicWhCheckDO;
import com.cssy.yd.logic.entity.LogicWhCheckItemDO;
import com.cssy.yd.logic.entity.LogicWhCheckMissionDO;
import com.cssy.yd.logic.entity.LogicWhCheckMissionItemDO;
import com.cssy.yd.logic.service.ILogicWHCheckService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.exception.SyCodeException;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicWHCheckService")
public class LogicWHCheckServiceImpl implements ILogicWHCheckService{
	@Resource(name = "logicWhCheckDAO")
	private LogicWhCheckDAO logicWhCheckDAO;
	
	@Resource(name = "logicWhCheckItemDAO")
	private LogicWhCheckItemDAO logicWhCheckItemDAO;
	
	@Resource(name = "logicWhCheckMissionItemDAO")
	private LogicWhCheckMissionItemDAO logicWhCheckMissionItemDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name = "logicWhCheckMissionDAO")
	private LogicWhCheckMissionDAO logicWhCheckMissionDAO;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWhCheckDO>  list=logicWhCheckDAO.find(scs);
		rs.setRecordCount(logicWhCheckDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		Integer i =logicWhCheckDAO.deleteLogicWhCheckDOByPrimaryKey(id);
		return i;
	}

	@Override
	public Integer update(LogicWhCheckDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicWhCheckDAO.updateLogicWhCheckDO(t);
	}

	@Override
	public LogicWhCheckDO save(LogicWhCheckDO t, User user) throws SyBaseException {
		t.setDcruser(user.getName());
		t.setDctime(new Date());
		t.setId(IDCodeTypeUtil.getId());
		logicWhCheckDAO.insertLogicWhCheckDO(t);
		return t;
	}

	@Override
	public LogicWhCheckDO findListById(String id) throws SyBaseException {
		return logicWhCheckDAO.findLogicWhCheckDOByPrimaryKey(id);
	}
	
	@Override
	public SearchResult findItemListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWhCheckItemDO>  list=logicWhCheckItemDAO.find(scs);
		rs.setRecordCount(logicWhCheckItemDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer deleteItem(String id) throws SyBaseException {
		Integer i =logicWhCheckItemDAO.deleteLogicWhCheckItemDOByPrimaryKey(id);
		return i;
	}

	@Override
	public Integer updateItem(LogicWhCheckItemDO t) throws SyBaseException {
		return logicWhCheckItemDAO.updateLogicWhCheckItemDO(t);
	}

	@Override
	public LogicWhCheckItemDO saveItem(LogicWhCheckItemDO t) throws SyBaseException {
		t.setId(IDCodeTypeUtil.getId());
		logicWhCheckItemDAO.insertLogicWhCheckItemDO(t);
		return t;
	}

	@Override
	public LogicWhCheckItemDO findItemListById(String id) throws SyBaseException {
		return logicWhCheckItemDAO.findLogicWhCheckItemDOByPrimaryKey(id);
	}

	@Override
	public List<LogicWhCheckItemDO> findItemListByCheckID(String checkID)
			throws SyBaseException {
		return logicWhCheckItemDAO.findItemByCheckID(checkID);
	}

	@Override
	public LogicWhCheckDO findLastedByOricode(String oriCode)
			throws SyBaseException {
		return logicWhCheckDAO.findLastedByOricode(oriCode);
	}

	@Override
	public void commit(HttpServletRequest request, LogicWhCheckDO curAP)
			throws SyBaseException, SyCodeException {
		try {
			String pdDate = request.getParameter("pdDate");
			String checkdata = request.getParameter("checkdata");
			String checkMissionCode = request.getParameter("oriCode");
			SearchConditions scs = new SearchConditions();
			scs.addCondition("checkmissioncode", checkMissionCode, SearchCondition.OP_EQUAL);
			List<LogicWhCheckMissionDO> cmlist = logicWhCheckMissionDAO.find(scs);
			LogicWhCheckMissionDO cmDO = new LogicWhCheckMissionDO();
			List<LogicWhCheckMissionItemDO> cmilist = new ArrayList<LogicWhCheckMissionItemDO>();
			if(cmlist.size()>0){
				cmDO = cmlist.get(0);
				cmilist = logicWhCheckMissionItemDAO.findItemByCheckMissionID(cmDO.getId());
			}
			List<LogicWhCheckItemDO> axxlist = JSONArray.parseArray(checkdata,LogicWhCheckItemDO.class);
			String id = IDCodeTypeUtil.getId();
			curAP.setCheckCode(syCodeService.getCode("PD","PD",SyCode.DATE_YYMM,5));
			curAP.setBillStatus(1);
			curAP.setDcruser(SessionUtil.getUser(request).getName());
			curAP.setDctime(new Date());
			curAP.setId(id);
			if (pdDate != null && !"".equals(pdDate)) {
				curAP.setPdDate(DateUtils.stringToDate(pdDate));
			}	
			logicWhCheckDAO.insertLogicWhCheckDO(curAP);
			for (LogicWhCheckItemDO lwcd : axxlist) {
				lwcd.setCheckID(id);
				lwcd.setId(IDCodeTypeUtil.getId());
				logicWhCheckItemDAO.insertLogicWhCheckItemDO(lwcd);
				for(LogicWhCheckMissionItemDO lwcmd : cmilist){
					String code = lwcmd.getCode();
					String batch = lwcmd.getBatch();
					if(code.equals(lwcd.getCode())&&batch.equals(lwcd.getBatch())){
						lwcmd.setQuantityWH(lwcd.getQuantityWH());
						lwcmd.setQuantityPD(lwcd.getQuantityPD());
						lwcmd.setQuantityCY(lwcd.getQuantityCY());
						lwcmd.setYdengQuantityWH(lwcd.getYdengQuantityWH());
						lwcmd.setYdengQuantityPD(lwcd.getYdengQuantityPD());
						lwcmd.setYdengQuantityCY(lwcd.getYdengQuantityCY());
						lwcmd.setCipinQuantityWH(lwcd.getCipinQuantityWH());
						lwcmd.setCipinQuantityPD(lwcd.getCipinQuantityPD());
						lwcmd.setCipinQuantityCY(lwcd.getCipinQuantityCY());
						lwcmd.setJiupinQuantityWH(lwcd.getJiupinQuantityWH());
						lwcmd.setJiupinQuantityPD(lwcd.getJiupinQuantityPD());
						lwcmd.setJiupinQuantityCY(lwcd.getJiupinQuantityCY());
						lwcmd.setFreezeQuantityWH(lwcd.getFreezeQuantityWH());
						lwcmd.setFreezeQuantityPD(lwcd.getFreezeQuantityPD());
						lwcmd.setFreezeQuantityCY(lwcd.getFreezeQuantityCY());
						lwcmd.setRemark(lwcd.getRemark());
						logicWhCheckMissionItemDAO.updateLogicWhCheckMissionItemDO(lwcmd);
					}
				}
			}
			cmDO.setAgent(curAP.getAgent());
			cmDO.setPdCount(cmDO.getPdCount()+1);
			cmDO.setBillStatus(2);
			logicWhCheckMissionDAO.updateLogicWhCheckMissionDO(cmDO);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}
	
	@Override
	public SearchResult findListDetatils(SearchConditions scs){
		SearchResult rs=new SearchResult(scs);
		rs.setRecordCount(logicWhCheckDAO.findListDetatilsCount(scs));
		rs.setResults(logicWhCheckDAO.findListDetatils(scs));
		return rs;
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

}
